---@class components
---@field staff_arachnidnexus_bind component_staff_arachnidnexus_bind # 实体绑定 ,主人和孩子 都需要添加这个组件

-- local function on_val(self, value)
    -- self.inst.replica.staff_arachnidnexus_bind:SetVal(value)
-- end

---@class component_staff_arachnidnexus_bind
---@field inst ent
---@field ismaster boolean # 是主人还是随从
---@field master_prefab_id string|nil # 主人的prefab_id
---@field uid string|nil # 自动在必要的时间点去生成
local staff_arachnidnexus_bind = Class(
---@param self component_staff_arachnidnexus_bind
---@param inst ent
function(self, inst)
    self.inst = inst
    -- self.val = 0
    self.ismaster = false
    self.master_prefab_id = nil
    self.uid = nil
end,
nil,
{
    -- val = on_val,
})

function staff_arachnidnexus_bind:OnSave()
    return {
        -- val = self.val
        ismaster = self.ismaster,
        master_prefab_id = self.master_prefab_id,
        uid = self.uid,
    }
end

function staff_arachnidnexus_bind:OnLoad(data)
    -- self.val = data.val or 0
    self.ismaster = data.ismaster or false
    self.master_prefab_id = data.master_prefab_id or self.inst.prefab
    self.uid = data.uid or nil

    ---当加载时,把自己添加到临时表里
    self:_PutMeInTUNING()
end

---(仅主人调用)初始化主人
---@param master_prefab_id string # 唯一ID,一般填主人的prefabID即可
function staff_arachnidnexus_bind:InitMaster(master_prefab_id)
    self.ismaster = true
    self.master_prefab_id = master_prefab_id
end

---(仅主人调用)为主人添加一个孩子
---@param child ent
function staff_arachnidnexus_bind:AddOneChild(child)
    ---尝试生成UID
    self:_GenUID()
    -- 将自己挂到TUNING中
    self:_PutMeInTUNING()
    -- 将孩子挂到TUNING中
    child.components.staff_arachnidnexus_bind.ismaster = false
    child.components.staff_arachnidnexus_bind.master_prefab_id = self.master_prefab_id
    child.components.staff_arachnidnexus_bind.uid = self.uid
    child.components.staff_arachnidnexus_bind:_PutMeInTUNING()
end

function staff_arachnidnexus_bind:_CreateTUNING()
    if TUNING.MOD_WEBBER_THE_TRAINER.bind == nil then
        TUNING.MOD_WEBBER_THE_TRAINER.bind = {}
    end
    if TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id] == nil then
        TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id] = {}
    end
    if TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid] == nil then
        TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid] = {}
    end
    if TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid].childs == nil then
        TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid].childs = {}
    end
end

---把我自己挂到TUNING表上
function staff_arachnidnexus_bind:_PutMeInTUNING()
    -- 仅当有uid, 即有child时, 才会把双方添加到临时表
    if self.uid ~= nil then
        self:_CreateTUNING()

        if self.ismaster then
            TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid].parent = self.inst
        else
            table.insert(TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid].childs,self.inst)
        end
    end
end

---生成UID, (master_prefab_id+timestamp), 仅第一次生成,后续不变
function staff_arachnidnexus_bind:_GenUID()
    if self.uid == nil then
        self.uid = (self.master_prefab_id) .. tostring(os.clock())
    end
end

---应当由孩子调用
---@return ent|nil
function staff_arachnidnexus_bind:GetMaster()
    if not self.ismaster and self.uid then
        return TUNING.MOD_WEBBER_THE_TRAINER.bind and TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id] and TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid] and TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid].parent or nil
    end
    return nil
end

---应当由主人调用
---@return ent[]|nil
function staff_arachnidnexus_bind:GetChildren()
    if self.ismaster and self.uid then
        return TUNING.MOD_WEBBER_THE_TRAINER.bind and TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id] and TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid] and TUNING.MOD_WEBBER_THE_TRAINER.bind[self.master_prefab_id][self.uid].childs or nil
    end
    return nil
end

return staff_arachnidnexus_bind